package com.baijiayun.liveuibase.error

import android.annotation.SuppressLint
import androidx.databinding.DataBindingUtil
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baijiayun.livecore.utils.DisplayUtils
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.ascamera.AsCameraListener
import com.baijiayun.liveuibase.base.getViewModel
import com.baijiayun.liveuibase.databinding.UibaseFragmentErrorBinding
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder
import kotlinx.android.synthetic.main.uibase_fragment_error.*
import kotlinx.android.synthetic.main.uibase_fragment_error.view.*

/**
 * Created by pengwenjian on 2020/11/16
 * Describe:
 */
class ErrorFragment: androidx.fragment.app.Fragment() {
    private lateinit var errorModel: ErrorViewModel
    private val listener by lazy{
        activity as AsCameraListener
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.uibase_fragment_error,container,false)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let {
            errorModel = it.getViewModel { ErrorViewModel() }
            val binding = DataBindingUtil.bind<UibaseFragmentErrorBinding>(view)
            binding?.lifecycleOwner = this
            binding?.errorModel = errorModel
            binding?.errorFragment = this
            view.setOnTouchListener { _, _ -> true }
            base_error_main_container.background = ThemeDataUtil.getCommonWindowBg(context)
        }
    }

    fun onNegative() {
        activity?.finish()
    }

    fun onPositive() {
        when (errorModel.handlerWay) {
            ErrorType.ERROR_HANDLE_RECONNECT, ErrorType.ERROR_HANDLE_REENTER -> {
                listener.reEnterRoom(false,!listener.isReconnect())
            }
            ErrorType.ERROR_HANDLE_CONFILICT -> {
                listener.reEnterRoom(false,!listener.isReconnect())
            }
            ErrorType.ERROR_HANDLE_FINISH -> {
                activity?.finish()
            }
            else -> {
                listener.dismissErrorDlg()
            }
        }
    }

    fun checkVisibility(content:String): Int {
        return if (TextUtils.isEmpty(content)) View.GONE else View.VISIBLE
    }

    enum class ErrorType {
        ERROR_HANDLE_RECONNECT, ERROR_HANDLE_REENTER, ERROR_HANDLE_NOTHING, ERROR_HANDLE_CONFILICT,ERROR_HANDLE_FINISH
    }
}